<?php
namespace app\controller;

use app\lib\TenXunCloudSmsModel;
use app\traits\ControllerTrait;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Sms\V20210111\Models\SendSmsRequest;
use TencentCloud\Sms\V20210111\SmsClient;

class TenXunCloudSms extends ControllerTrait {

    public function sendShortMessage(){
        $secretId = 'AKIDZ0DBuOcbKu9AxUlmX2HH1VkjSv98rKXU';
        $secretKey = 'kFUTDhvtb4HEuJRItDG2XmpY3MRGqBH8';
        $smsSdkAppId = '1400535737';
        $signName = '流行因乐';
        $templateId = '998402';
        $TemplateParamSet = json_encode(rand(100000,999999));

        $param = $this->request->param();
        $rule = [
            "phoneNumberSet|电话号码" => "require",
        ];
        $this->validate($param, $rule);
        try {
            $cred = new Credential($secretId,$secretKey);
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("sms.tencentcloudapi.com");

            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new SmsClient($cred, "ap-guangzhou", $clientProfile);

            $req = new SendSmsRequest();

            $params = array(
                "Action" => "SendSms",
                "Version" => "2021-01-11",
                "PhoneNumberSet" => array("+86{$param['phoneNumberSet']}"),
                "SmsSdkAppId" => $smsSdkAppId,
                "SignName" => $signName,
                "TemplateId" => $templateId,
                "TemplateParamSet" => array($TemplateParamSet)
            );
            $req->fromJsonString(json_encode($params));

            $resp = $client->SendSms($req);

            print_r(json($resp->toJsonString()));
        } catch (TencentCloudSDKException $e) {
            echo $e;
        }
    }
}
